/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#ifndef SQLLINEEDIT_H
#define SQLLINEEDIT_H

#include <QLineEdit>
#include <QDoubleValidator>
#include <QSqlRecord>
#include "win32def.h"

namespace SQL
{class Query;}

class KONTAMABIL_EXPORTS LineEdit : public QLineEdit
{
	Q_OBJECT
	Q_PROPERTY(bool handleFocus READ handleFocus WRITE setHandleFocus)
	Q_PROPERTY(QString queryName READ queryName WRITE setQueryName)
	Q_PROPERTY(QString fieldName READ fieldName WRITE setFieldName)
	Q_PROPERTY(bool useDoubleValidator READ useDoubleValidator WRITE setUseDoubleValidator)
	Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
	Q_PROPERTY(double minValue READ minValue WRITE setMinValue)
	Q_PROPERTY(QString minValueFieldName READ minValueFieldName WRITE setMinValueFieldName)
	Q_PROPERTY(double maxValue READ maxValue WRITE setMaxValue)
	Q_PROPERTY(QString maxValueFieldName READ maxValueFieldName WRITE setMaxValueFieldName)
	Q_PROPERTY(QString defaultValue READ defaultValue WRITE setDefaultValue)

public:
	LineEdit(QWidget *parent = 0);
	~LineEdit();

	const QString & queryName();
	void setQueryName(const QString & queryName);

	const QString & fieldName();
	void setFieldName(const QString & fieldName);

	bool handleFocus();
	void setHandleFocus(bool handleFocus);

	bool useDoubleValidator();
	void setUseDoubleValidator(bool useDoubleValidator);

	int decimals();
	void setDecimals(int decimals);

	double minValue();
	void setMinValue(double minValue);

	QString minValueFieldName();
	void setMinValueFieldName(const QString & minValueFieldName);

	double maxValue();
	void setMaxValue(double maxValue);

	QString maxValueFieldName();
	void setMaxValueFieldName(const QString & maxValueFieldName);
	
	QString defaultValue();
	void setDefaultValue(const QString & defaultValue);

private:
	void setupValidator();

signals:
	void escapePressed();
	void focusIn();
	void focusOut();

protected:
	void keyPressEvent(QKeyEvent * event);
	void focusInEvent(QFocusEvent * event);
	void focusOutEvent(QFocusEvent * event);

private slots:
	void recordChanged(const QSqlRecord&);
	void setReadWrite();
	void setReadOnly();
	void bindValue();

private:
	QString m_queryName;
	QString m_fieldName;
	SQL::Query * m_query;
	bool m_handleFocus;
	bool m_useDoubleValidator;
	int m_decimals;
	double m_minValue;
	QString m_minValueFieldName;
	double m_maxValue;
	QString m_maxValueFieldName;
	QDoubleValidator * m_doubleValidator;
	QString m_defaultValue;
};

#endif
